#!/usr/bin/env Rscript
## This file runs BRS on the voting data

# BRS parameters ------------------
numMine <- 5000L  # number of rules to be used in SA_patternbased and also the output of generate_rules
numIter <- 10000L  # number of iterations in each chain
numChain <- 10L  # number of chains in the simulated annealing search algorithm
supp <- 5L  # minimum support
maxLen <- 3L  # maxmum length of a pattern
alpha_1 <- 500L  # alpha_+
beta_1 <- 1L  # beta_+
alpha_2 <- 500L  # alpha_-
beta_2 <- 1L  # beta_-
trainProp <- 1   # proportion of data to use as training
lambda <- 1
nu <- 1/1000    # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper

reps <- 100L  # number of bootstrap reps

set.seed(123)
seeds <- sample.int(1e5, size=reps)

# BRS function does not accommodate internal parallelization
ls <- foreach (i=1:reps) %dopar% {
  # see BRS package documentation for details about the BRS function
  BRS(df=X, Y=Y,  
      maxLen=maxLen, trainProp=trainProp,
      numIter=numIter, numChain=numChain,
      supp=supp, numMine=numMine,
      alpha_1=alpha_1, alpha_2=alpha_2,
      beta_1=beta_1, beta_2=beta_2,
      prior_type="poisson",
      alpha_l=NULL, beta_l=NULL,  # not used for the poisson version
      lambda=lambda, nu=nu,    # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
      bootstrap=T, reps=1L,  # when boostrap=T and reps=1, will run BRS on a random sample (w/ replacement) of the same size as original data
      print=F,
      seed=seeds[i])
}

# recombine output
l1 <- lapply(1:length(ls), function(x) ls[[x]][[1]][[1]])
l2 <- lapply(1:length(ls), function(x) ls[[x]][[2]][[1]])

out <- list("Rule Sets"=l1, "Indices"=l2, NULL)

save(out, file="vote/out/vote_out_pois.rda")